<template>
  <div class="cesium-viewer-wapper">
    <div id="cesium-viewer" />
    <viewerSettingSidebar top="10em" :viewerSource="viewerSource" />
    <div class="tool-box-top-left">
      <!-- <div class="select-box-wrapper"><select-box :options="flyOptions" style="margin-right: 0;" /></div> -->
      <!-- <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.translucencyEnabled">translucencyEnabled</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.fadeByDistance">fadeByDistance</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="viewModel.showVectorData">showVectorData</a-checkbox></div> -->
      <div class="select-box-wrapper"><SliderBar v-model:value="height" :min="-1000" :max="1000" :step="10">height</SliderBar></div>
    </div>
    <GlobeRotater showTime :viewerSource="viewerSource" />
    <MousePosition show :viewerSource="viewerSource" />
  </div>
</template>

<script>
import * as Cesium from '@cesiumjs';
import { defineComponent, ref, reactive, watch, toRaw } from 'vue';
import { SelectBox, SliderBar } from '@components/widgets';
import { MousePosition, viewerSettingSidebar, GlobeRotater } from '@g3x/g3v/ui';
import { G3V } from '@g3x';
import { viewerMixin } from '@mixins';
// import merge from '@g3x/utils/merge';

const $g3vOptions = {
  defaultViewRectangle: G3V.defaultViews.global.china.rectangle,
  viewers: [{
    dom: 'cesium-viewer',
    name: 'viewer1',
    title: 'viewer#01',
    viewer: {
      imagery: { type: 'default', options: { brightness: 1, show: false } },
      imageryProvider: null,
      terrain: { type: 'default', options: { requestVertexNormals: true, requestWaterMask: true } },
      terrainProvider: null,
      shadows: false,
      scene3DOnly: true,
      // sceneMode: Cesium.SceneMode.SCENE2D,
      animation: false
    },
    layers: [
      { type: 'arcgisImagery', options: { brightness: 1, show: false } },
      { type: 'tdtImage', options: { brightness: 1, show: false } },
      { type: 'tdtVector', options: { brightness: 1, show: false } },
      { type: 'tdtTerrain', options: { brightness: 1, show: false } },
      { type: 'osmImagery', options: { brightness: 1, show: false } },
      { type: 'arcgisStreetMap', options: { brightness: 1, show: false } },
      { type: 'bingMapsImagery', options: { brightness: 1, show: false } },
      { type: 'bingMapsAeriaAnno', options: { brightness: 1, show: false } },
      { type: 'bingMapsRoad', options: { brightness: 1, show: false } },
      { type: 'tdtIboMap', options: { brightness: 1, show: false } },
      { type: 'tdtImgAnno', options: { brightness: 1, show: false } },
      { type: 'tdtTerrainAnno', options: { brightness: 1, show: false } },
      { type: 'tdtVecAnno', options: { brightness: 1, show: false } },
      { type: 'tdtVecAnnoEn', options: { brightness: 1, show: false } },
      { type: 'darkMask', options: { brightness: 1, alpha: 0.9, show: true } },
      { type: 'graticule', options: { brightness: 1, show: true } }
    ],
    globe: {
      depthTestAgainstTerrain: false,
      enableLighting: true //启用以太阳为光源的地球
    },
    bottomContainer: false,
    navigation: {
      // defaultResetView: G3V.defaultViews.global.china.rectangle ,
      defaultResetView: null //flyHome
    },
    shortcutAction: false,
    hideLayers: false
  }]
};

export default defineComponent({
  name: 'bloom', //
  mixins: [viewerMixin],
  components: { SelectBox, SliderBar, viewerSettingSidebar, GlobeRotater, MousePosition },
  props: {},
  setup() {
    return {};
  },
  data() {
    return {
      height: 0
    };
  },
  computed: {},
  watch: {},
  methods: {
    g3vOptionsGet(original) {
      // const g3vOptions = merge($g3vOptions, true);
      return $g3vOptions;
    },
    onViewerInited() {
      // console.log('gdp onViewerInited');
    },
    initEffects() {
      const vm = this;
      const { viewer } = vm;
      const scene = viewer.scene;

      var tileset = new Cesium.Cesium3DTileset({
        url: '//zzgis.com/cdn/3d/3dtiles/jzw-hefei/tileset.json',
      });

      tileset.readyPromise
        .then(function(tileset) {
          scene.primitives.add(tileset);
          viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius * 2.0));
        })
        .otherwise(function(error) {
          console.error(error);
        });

      const bloom = scene.postProcessStages.bloom;
      bloom.enabled = true;
      bloom.uniforms.glowOnly = false;
      bloom.uniforms.contrast = 128;
      bloom.uniforms.delta = 1;
      bloom.uniforms.brightness = 0.3;
      bloom.uniforms.stepSize = 5;
      bloom.uniforms.sigma = 3.75;

      watch(()=>vm.height, (height) => {
        height = Number(height);
        if (isNaN(height)) {
          return;
        }
        var cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
        var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
        var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
        var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
        tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
      });
    }
  }
});
</script>

<style lang="less">
.cesium-viewer-wapper {
  .tool-box-top-left {
    min-width: 15em;
  }
}
</style>
